package com.project.light.controller;


import com.project.light.dto.ConfigDto;
import com.project.light.dto.LoginParam;
import com.project.light.dto.LoginResult;
import com.project.light.entity.System;
import com.project.light.service.LoginService;
import com.project.light.util.HttpUtil;
import com.project.light.util.RestResult;
import com.project.light.util.TokenEncrypt;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/lighting-rest/api/v1/")
public class LoginController {

    @Resource
    ConfigDto configDto;
    @Resource
    LoginService loginService;

    /**
     * 登录
     */
    @PostMapping("DoLogin/post")
    public RestResult postLogin(@RequestBody LoginParam param) {
        LoginResult result;
        try {
            String url = param.getHost() + "/lighting-rest/api/v1/DoLogin" + "?sysId=" + param.getSysId() + "&sysname=" + param.getSysname() + "&password=" + param.getPassword();
            HttpUtil httpUtil = new HttpUtil(url, configDto.getTimeout());
            result = httpUtil.doLogin();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
        return RestResult.ok(result);
    }

    /**
     * 登录
     */
    @GetMapping("DoLogin")
    public RestResult login(String sysId, String sysname, String password) {
        if (StringUtils.isEmpty(sysId)) {
            return RestResult.error("sysId为空");
        }
        if (StringUtils.isEmpty(sysname)) {
            return RestResult.error("sysname为空");
        }
        if (StringUtils.isEmpty(password)) {
            return RestResult.error("password为空");
        }
        LoginResult loginResult = new LoginResult();
        try {
            System system = loginService.getBySys(sysId, sysname);
            if (system == null) {
                return RestResult.error("sysId或sysname不正确");
            } else {
                BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                if (!passwordEncoder.matches(password, system.getPassword())) {
                    return RestResult.error("密码不正确");
                }
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                system.setToken(TokenEncrypt.sha1(sysId + sysname + DateFormatUtils.format(calendar.getTime(), "yyyyMMddHHmmss")));
                system.setExpirationDate(calendar.getTime());
                system.setUpdatedUser(sysId);
                system.setUpdatedDate(new Date());
                loginService.updateSystem(system);
//                Users users = loginService.getUserBySys(sysname);
//                if (users != null) {
//                    users.setToken(TokenEncrypt.sha1(sysId + sysname + DateFormatUtils.format(calendar.getTime(), "yyyyMMddHHmmss")));
//                    users.setExpireTime(calendar.getTime());
//                    users.setLoginTime(new Date());
//                    users.setUpdatedUser(sysId);
//                    users.setUpdatedDate(new Date());
//                    loginService.updateUsers(users);
//                }
                loginResult.setToken(system.getToken());
                loginResult.setExpirationDate(system.getExpirationDate());
                return RestResult.ok(loginResult);
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    @PostMapping("InsertLogin")
    public RestResult insertLogin(@RequestBody LoginParam param) {
        if (StringUtils.isEmpty(param.getSysId())) {
            return RestResult.error("sysId为空");
        }
        if (StringUtils.isEmpty(param.getSysname())) {
            return RestResult.error("sysname为空");
        }
        if (StringUtils.isEmpty(param.getPassword())) {
            return RestResult.error("password为空");
        }
        try {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            System system = loginService.getBySys(param.getSysId(), param.getSysname());
            if (system == null) {
                system = new System();
                system.setSysId(param.getSysId());
                system.setSysName(param.getSysname());
                system.setPassword(passwordEncoder.encode(param.getPassword()));
                system.setCreatedUser(param.getSysId());
                system.setCreatedDate(new Date());
                system.setUpdatedUser(param.getSysId());
                system.setUpdatedDate(new Date());
                system.setDeleteFlag(0);
                system.setToken(TokenEncrypt.sha1(param.getSysId() + param.getSysname()) + DateFormatUtils.format(calendar.getTime(), "yyyyMMdd"));
                system.setExpirationDate(calendar.getTime());
                loginService.insertSystem(system);
            } else {
                system.setCreatedUser(param.getSysId());
                system.setCreatedDate(new Date());
                system.setUpdatedUser(param.getSysId());
                system.setUpdatedDate(new Date());
                system.setDeleteFlag(0);
                system.setToken(TokenEncrypt.sha1(param.getSysId() + param.getSysname()) + param.getPassword() + DateFormatUtils.format(calendar.getTime(), "yyyyMMdd"));
                system.setExpirationDate(calendar.getTime());
                loginService.updateSystem(system);
            }
//            Users users = loginService.getUserBySys(param.getSysname());
//            if (users == null) {
//                users = new Users();
//                users.setUserName(param.getSysname());
//                users.setPassword(passwordEncoder.encode(param.getPassword()));
//                users.setCreatedUser(param.getSysId());
//                users.setCreatedDate(new Date());
//                users.setUpdatedUser(param.getSysId());
//                users.setUpdatedDate(new Date());
//                users.setDeleteFlag(0);
//                users.setToken(TokenEncrypt.sha1(param.getSysId() + param.getSysname()) + param.getPassword() + DateFormatUtils.format(calendar.getTime(), "yyyyMMdd"));
//                users.setExpireTime(calendar.getTime());
//                users.setLoginTime(new Date());
//                loginService.insertUsers(users);
//            } else {
//                users.setCreatedUser(param.getSysId());
//                users.setCreatedDate(new Date());
//                users.setUpdatedUser(param.getSysId());
//                users.setUpdatedDate(new Date());
//                users.setDeleteFlag(0);
//                users.setToken(TokenEncrypt.sha1(param.getSysId() + param.getSysname()) + param.getPassword() + DateFormatUtils.format(calendar.getTime(), "yyyyMMdd"));
//                users.setExpireTime(calendar.getTime());
//                users.setLoginTime(new Date());
//                loginService.updateUsers(users);
//            }
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }
}
